//
//  EmoticonGroupModel.swift
//  01-表情键盘
//
//  Created by 陈伟 on 15/10/25.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import Foundation

class EmoticonGroupModel: NSObject {
    
    var id: String?
    
    var group_name_cn: String?
    
    lazy var emoticons = [EmoticonModel]()
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        group_name_cn = (dict["group_name_cn"] as? String)
        id = dict["id"] as? String
        
        if let tempArray = dict["emoticons"] as? [[String: String]] {
            var array = [EmoticonModel]()
            var index = 0
            for var d in tempArray {
                if let png = d["png"] {
                    d["png"] = id! + "/" + png
                    let emoticon = EmoticonModel(dict: d)
                    array.append(emoticon)
                }else{
                    let emoticon = EmoticonModel(dict: d)
                    array.append(emoticon)
                }
                index++
                if (index == 20) {
                    array.append(EmoticonModel(isRemoved: true))
                    index = 0
                }
            }
            emoticons = array
        }
        appendEmptyEmoticon()
    }
    
    func appendEmptyEmoticon() {
        let count = emoticons.count % 21
        
        if emoticons.count > 0 && count == 0 {
            return
        }
        
        for _ in count..<20 {
            emoticons.append(EmoticonModel(isEmpty: true))
        }

        emoticons.append(EmoticonModel(isRemoved: true))
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let keys = ["id", "group_name_cn", "emoticons"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
